{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Jupyter Notebook预览](https://cdn.jsdelivr.net/gh/eternidad33/picbed/img/upyter%E9%A2%84%E8%A7%88.png)](https://nbviewer.jupyter.org/github/eternidad33/csp/blob/master/Python/题解汇总/第1题题解汇总.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 目录\n",
    "\n",
    "- [出现次数最多的数](#出现次数最多的数)\n",
    "- [相反数](#相反数)\n",
    "- [相邻数对](#相邻数对)\n",
    "- [门禁系统](#门禁系统)\n",
    "- [图像旋转](#图像旋转)\n",
    "- [数列分段](#数列分段)\n",
    "- [数位之和](#数位之和)\n",
    "- [折点计数](#折点计数)\n",
    "- [最大波动](#最大波动)\n",
    "- [中间数](#中间数)\n",
    "- [分蛋糕](#分蛋糕)\n",
    "- [打酱油](#打酱油)\n",
    "- [最小差值](#最小差值)\n",
    "- [跳一跳](#跳一跳)\n",
    "- [卖菜](#卖菜)\n",
    "- [小明上学](#小明上学)\n",
    "- [小中大](#小中大)\n",
    "- [小明种苹果](#小明种苹果)\n",
    "- [报数](#报数)\n",
    "- [线性分类器](#线性分类器)\n",
    "- [称检测点查询](#称检测点查询)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 出现次数最多的数\n",
    "\n",
    "试题编号：\t201312-1  \n",
    "试题名称：\t出现次数最多的数\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "n=6\n",
    "l=[10, 1, 10, 20, 30, 20]\n",
    "# n = int(input())\n",
    "# l = list(map(int, input().split()))\n",
    "# 先将数据转成集合，然后再转成列表\n",
    "s = list(set(l))\n",
    "# t用于记录s中元素在l出现的次数\n",
    "t = []\n",
    "for x in s:\n",
    "    t.append(l.count(x))\n",
    "# t中最大值对应的索引即为所求\n",
    "print(s[t.index(max(t))])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 相反数\n",
    "\n",
    "试题编号：\t201403-1  \n",
    "试题名称：\t相反数\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# n = int(input())\n",
    "n = 5\n",
    "s=[1,2,3,-1,-2]\n",
    "# s = input().split()\n",
    "num = []\n",
    "for i in range(n):\n",
    "    s[i] = int(s[i])\n",
    "    if s[i] not in num:\n",
    "        num.append(s[i])\n",
    "count = 0\n",
    "for i in num:\n",
    "    for j in num:\n",
    "        if i + j == 0:\n",
    "            count += 1\n",
    "print(int(count / 2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 相邻数对\n",
    "\n",
    "试题编号：\t201409-1  \n",
    "试题名称：\t相邻数对\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# n = int(input())\n",
    "n=6\n",
    "s=[10,2,6,3,7,8]\n",
    "# s = input().split()\n",
    "num = []\n",
    "for i in range(n):\n",
    "    s[i] = int(s[i])\n",
    "    if s[i] not in num:\n",
    "        num.append(s[i])\n",
    "count = 0\n",
    "for i in num:\n",
    "    for j in num:\n",
    "        if i - j == 1:\n",
    "            count += 1\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 门禁系统\n",
    "\n",
    "试题编号：\t201412-1  \n",
    "试题名称：\t门禁系统\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 2 3 1 "
     ]
    }
   ],
   "source": [
    "n=5\n",
    "s=[1, 2, 1, 1, 3]\n",
    "# n = int(input())\n",
    "# s = input().split()\n",
    "a = []\n",
    "for i in range(n):\n",
    "    s[i] = int(s[i])\n",
    "    a.append(s[i])\n",
    "c = []\n",
    "for j in range(len(a)):\n",
    "    count = 1\n",
    "    for k in range(j):\n",
    "        if a[j] == a[k]:\n",
    "            count += 1\n",
    "    c.append(count)\n",
    "for t in c:\n",
    "    print(t, end=\" \")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图像旋转\n",
    "\n",
    "试题编号：\t201503-1  \n",
    "试题名称：\t图像旋转\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tuxiangxuanzhuan(row, col):\n",
    "    img = []\n",
    "    for i in range(row):\n",
    "        a = list(map(int, input().split()))\n",
    "        img.append(a)\n",
    "    for i in range(col):\n",
    "        for j in range(row):\n",
    "            print(img[j][col - 1 - i], end=\" \")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数列分段\n",
    "\n",
    "试题编号：\t201509-1  \n",
    "试题名称：\t数列分段\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "n=8\n",
    "a=[8, 8, 8, 0, 12, 12, 8, 0]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "count = 1\n",
    "for i in range(1, n):\n",
    "    if a[i] != a[i - 1]:\n",
    "        count += 1\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数位之和\n",
    "\n",
    "试题编号：\t201512-1  \n",
    "试题名称：\t数位之和\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "# n = input()\n",
    "n=\"20151220\"\n",
    "sum_n = 0\n",
    "for i in n:\n",
    "    i = int(i)\n",
    "    sum_n += i\n",
    "print(sum_n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 折点计数\n",
    "\n",
    "试题编号：\t201604-1  \n",
    "试题名称：\t折点计数\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "n=7\n",
    "a=[5,4,1,2,3,6,4]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "count = 0\n",
    "for i in range(1, n - 1):\n",
    "    if a[i - 1] > a[i] and a[i + 1] > a[i]:\n",
    "        count += 1\n",
    "    elif a[i - 1] < a[i] and a[i + 1] < a[i]:\n",
    "        count += 1\n",
    "    else:\n",
    "        pass\n",
    "print(count)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 最大波动\n",
    "\n",
    "试题编号：\t201609-1  \n",
    "试题名称：\t最大波动\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "n=6\n",
    "a=[2, 5, 5, 7, 3, 5]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "b = 0\n",
    "for i in range(n - 1):\n",
    "    temp = abs(a[i] - a[i + 1])\n",
    "    if temp > b:\n",
    "        b = temp\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 中间数\n",
    "\n",
    "试题编号：\t201612-1  \n",
    "试题名称：\t中间数\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "n=6\n",
    "a=[2, 6, 5, 6, 3, 5]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "t = False\n",
    "for i in a:\n",
    "    max_num, min_num = 0, 0\n",
    "    for j in a:\n",
    "        if j < i:\n",
    "            min_num += 1\n",
    "        if j > i:\n",
    "            max_num += 1\n",
    "    if min_num == max_num:\n",
    "        print(i)\n",
    "        t = True\n",
    "        break\n",
    "if not t:\n",
    "    print(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分蛋糕\n",
    "\n",
    "试题编号：\t201703-1  \n",
    "试题名称：\t分蛋糕\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "n,k=6,9\n",
    "a=[2, 6, 5, 6, 3, 5]\n",
    "# n, k = map(int, input().split())\n",
    "# a = list(map(int, input().split()))\n",
    "count = 0\n",
    "t = 0\n",
    "for i in a:\n",
    "    t += i\n",
    "    if t >= k:\n",
    "        count += 1\n",
    "        t = 0\n",
    "if t != 0:\n",
    "    count += 1\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打酱油\n",
    "\n",
    "试题编号：\t201709-1  \n",
    "试题名称：\t打酱油\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# n = int(input())\n",
    "n=40\n",
    "n1 = n // 50\n",
    "n2 = (n % 50) // 30\n",
    "n3 = (n - (50 * n1 + 30 * n2)) / 10\n",
    "n4 = 7 * n1 + 4 * n2 + n3\n",
    "print(int(n4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 最小差值\n",
    "\n",
    "试题编号：\t201712-1  \n",
    "试题名称：\t最小差值\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "n=5\n",
    "a=[1, 5, 4, 8, 20]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "minn = abs(a[1] - a[0])\n",
    "for i in range(n):\n",
    "    for j in range(n):\n",
    "        if i != j:\n",
    "            temp = abs(a[i] - a[j])\n",
    "            if temp < minn:\n",
    "                minn = temp\n",
    "print(minn)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 跳一跳\n",
    "\n",
    "试题编号：\t201803-1  \n",
    "试题名称：\t跳一跳\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n"
     ]
    }
   ],
   "source": [
    "a=[1, 1, 2, 2, 2, 1, 1, 2, 2, 0]\n",
    "# a = list(map(int, input().split()))\n",
    "n = len(a)\n",
    "score, temp = 0, 2\n",
    "for i in range(n):\n",
    "    if a[i] == 0:\n",
    "        break\n",
    "    elif a[i] == 1:\n",
    "        score += 1\n",
    "        temp = 2\n",
    "    else:\n",
    "        if a[i - 1] == 1 or i == 0:\n",
    "            score += 2\n",
    "            temp += 2\n",
    "        else:\n",
    "            score += temp\n",
    "            temp += 2\n",
    "print(score)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 卖菜\n",
    "\n",
    "试题编号：\t201809-1  \n",
    "试题名称：\t卖菜\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 2 1 3 4 9 10 13 "
     ]
    }
   ],
   "source": [
    "n=8\n",
    "a=[4, 1, 3, 1, 6, 5, 17, 9]\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "b = []\n",
    "for i in range(n):\n",
    "    if i == 0:\n",
    "        temp = (a[i] + a[i + 1]) // 2\n",
    "        b.append(temp)\n",
    "    elif i == n - 1:\n",
    "        temp = (a[i] + a[i - 1]) // 2\n",
    "        b.append(temp)\n",
    "    else:\n",
    "        temp = (a[i - 1] + a[i] + a[i + 1]) // 3\n",
    "        b.append(temp)\n",
    "for i in b:\n",
    "    print(i, end=\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小明上学\n",
    "\n",
    "试题编号：\t201812-1  \n",
    "试题名称：\t小明上学\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xiaomingshangxue():\n",
    "    r, y, g = list(map(int, input().split()))\n",
    "    n = int(input())\n",
    "    second = 0\n",
    "    for i in range(n):\n",
    "        k, t = list(map(int, input().split()))\n",
    "        if k == 0 or k == 1:\n",
    "            second += t\n",
    "        elif k == 2:\n",
    "            second += (t + r)\n",
    "        else:\n",
    "            pass\n",
    "    print(second)\n",
    "\n",
    "\n",
    "# xiaomingshangxue()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小中大\n",
    "\n",
    "试题编号：\t201903-1  \n",
    "试题名称：\t小中大\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 2 -1\n"
     ]
    }
   ],
   "source": [
    "n=3\n",
    "a=[-1,2,4]\n",
    "\n",
    "# n = int(input())\n",
    "# a = list(map(int, input().split()))\n",
    "\n",
    "mid = (a[(n - 1) // 2] + a[n // 2]) / 2\n",
    "if int(mid) == mid:\n",
    "    mid = int(mid)\n",
    "\n",
    "print(\"{} {} {}\".format(max(a), round(mid, 1), min(a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小明种苹果\n",
    "\n",
    "试题编号：\t201909-1  \n",
    "试题名称：\t小明种苹果\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xiaoMingZhongPingGuo():\n",
    "    N, M = map(int, input().split())\n",
    "    T, k, P = 0, 0, 0\n",
    "    for i in range(N):\n",
    "        s = list(map(int, input().split()))\n",
    "        a0, am = s[0], s[1:]\n",
    "        T += a0\n",
    "        am = map(abs, am)\n",
    "        p = sum(am)\n",
    "        T -= p\n",
    "        if p > P:\n",
    "            P = p\n",
    "            k = i + 1\n",
    "    print(T, k, P)\n",
    "\n",
    "\n",
    "# xiaoMingZhongPingGuo()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 报数\n",
    "\n",
    "试题编号：\t201912-1  \n",
    "试题名称：\t报数\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "n=20\n",
    "# n = int(input())\n",
    "count = 0\n",
    "i = 0\n",
    "l = [0, 0, 0, 0]\n",
    "while count != n:\n",
    "    i += 1\n",
    "    if i % 7 == 0 or str(i).__contains__('7'):\n",
    "        b = i % 4\n",
    "        l[b] += 1\n",
    "    else:\n",
    "        count += 1\n",
    "print(l[1])\n",
    "print(l[2])\n",
    "print(l[3])\n",
    "print(l[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性分类器\n",
    "\n",
    "试题编号：\t202006-1  \n",
    "试题名称：\t线性分类器\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xianXingFenLeiQi():\n",
    "    n, m = map(int, input().split())\n",
    "    datas = []\n",
    "    # 录入数据\n",
    "    for i in range(n):\n",
    "        data = input().split()\n",
    "        data[0] = int(data[0])\n",
    "        data[1] = int(data[1])\n",
    "        datas.append(data)\n",
    "    flags = []\n",
    "    for i in range(m):\n",
    "        line = list(map(int, input().split()))\n",
    "        flag = True\n",
    "        # 判断第一个点的位置\n",
    "        r1 = datas[0][0] * line[1] + datas[0][1] * line[2] + line[0]\n",
    "        if r1 > 0 and datas[0][2] == 'A':\n",
    "            AisUp = True\n",
    "        elif r1 < 0 and datas[0][2] != 'A':\n",
    "            AisUp = True\n",
    "        else:\n",
    "            AisUp = False\n",
    "\n",
    "        for j in range(1, n):\n",
    "            result = datas[j][0] * line[1] + datas[j][1] * line[2] + line[0]\n",
    "            if AisUp:\n",
    "                if result > 0 and datas[j][2] == 'A':\n",
    "                    flag = True\n",
    "                elif result > 0 and datas[j][2] == 'B':\n",
    "                    flag = False\n",
    "                    break\n",
    "                elif result < 0 and datas[j][2] == \"A\":\n",
    "                    flag = False\n",
    "                    break\n",
    "                elif result < 0 and datas[j][2] == \"B\":\n",
    "                    flag = True\n",
    "            else:\n",
    "                if result < 0 and datas[j][2] == 'A':\n",
    "                    flag = True\n",
    "                elif result < 0 and datas[j][2] == 'B':\n",
    "                    flag = False\n",
    "                    break\n",
    "                elif result > 0 and datas[j][2] == \"A\":\n",
    "                    flag = False\n",
    "                    break\n",
    "                elif result > 0 and datas[j][2] == \"B\":\n",
    "                    flag = True\n",
    "        flags.append(flag)\n",
    "    for flag in flags:\n",
    "        if flag:\n",
    "            print(\"Yes\")\n",
    "        else:\n",
    "            print(\"No\")\n",
    "\n",
    "\n",
    "# xianXingFenLeiQi()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 称检测点查询\n",
    "\n",
    "试题编号：\t202009-1  \n",
    "试题名称：\t称检测点查询\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 称检测点查询():\n",
    "    n, X, Y = map(int, input().split())\n",
    "    w = []\n",
    "    s = []\n",
    "    for i in range(n):\n",
    "        x, y = map(int, input().split())\n",
    "        si = (x - X) ** 2 + (y - Y) ** 2\n",
    "        s.append(si)\n",
    "        w.append([i + 1, si])\n",
    "    s = sorted(s)\n",
    "    da = []\n",
    "    for i in range(3):\n",
    "        for j in range(n):\n",
    "            flag = False\n",
    "            if w[j][1] == s[i]:\n",
    "                if w[j][0] not in da:\n",
    "                    da.append(w[j][0])\n",
    "                    flag = True\n",
    "                    break\n",
    "                if flag:\n",
    "                    break\n",
    "    for d in da:\n",
    "        print(d)\n",
    "\n",
    "\n",
    "# 称检测点查询()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
